#include "task.h"

static unsigned char cntr_soc = 0;
static unsigned char cntr_key = 0;
static unsigned char cntr_dsp = 0;
static unsigned char cntr_temp = 0;
static unsigned char cntr_ctr = 0;
static unsigned int  cntr_6s = 0;
static unsigned char  cntr_record = 0;
static unsigned char  cntr_output = 0;

static unsigned char ui = 0;					//主界面
static unsigned char dis_unit = 0;				//距离单位，默认为cm
static unsigned char ui_para = 0;				//参数界面
static unsigned char ui_factory = 0;			//工厂模式

static unsigned int distance = 0;				//距离
static unsigned int temp = 0;					//温度
static unsigned int record_distance[35];		//距离记录

static unsigned char para_dis = 40;				//默认距离参数:40cm
static char para_temp = 30;						//默认温度参数:30℃
static char cali_value = 0;						//默认校准值:0cm
static unsigned int  tran_speed = 340;			//默认传输速度:340m/s
static unsigned char DAC_limit = 10;			//默认DAC输出下限值:1.0V，放大10倍

bit f_record = 0;

//1ms中断一次@12MHz
void initSysTick(void)
{
    AUXR |= 0x04;
    T2L = 0x20;
	T2H = 0xD1;
	AUXR |= 0x10;
    IE2 |= 0x04;
	EA = 1;
}


//初始化板级外设
void initBsp(void)
{
	BUZ(0x00);
	LED(0xFF);
	InitDsp();
	InitKeyStruct();
	InitPCF8591();	
	InitSonicTimer();
}

//定时器中断服务函数
void isrSysTick(void)    interrupt 12
{
	GetDisplayStruct()->SegDisplay();
	
	if(cntr_temp < 200){
		++cntr_temp;
	}

	if(cntr_soc < 100){
		++cntr_soc;
	}
	if(cntr_key < 10){
		++cntr_key;
	}

	if(cntr_dsp < 50){
		++cntr_dsp;
	}
	if(cntr_ctr < 100){
		++cntr_ctr;
	}
	if(cntr_record < 200){
		++cntr_record;
	}
	if(cntr_output < 200){
		++cntr_output;
	}
	
	
	if(cntr_6s > 0){
		--cntr_6s;
	}
}

//
void taskControl(void)
{
	static unsigned char led = 0xff;
	static unsigned char buz = 0x00;

	//100ms 刷新
	if(cntr_ctr < 100)
		return;
	cntr_ctr = 0;
	
	/* LED */
	if(ui == 0){
		if(distance >= 255){
			led = ~0xff;
		}else{
			led = ~distance;
		}
	}else if(ui == 1){
		led &= ~0x80;
		led |= 0x7f;
	}else if(ui == 2){
		led ^= 1;
		led |= 0xfe;
	}
	/* 继电器 */
	if((((para_dis - 5) <= distance)&&(distance <= (para_dis + 5)))&&(temp <= para_temp*10)){
		buz |= (1 << 4);
	}else{
		buz = 0;
	}
	
	LED(led);
	BUZ(buz);
}

//
void taskCollect(void)
{
	unsigned int tmp = 0;
	float temp_s = 0;
	
	/* 采集电压数据  */
	if(cntr_temp == 200){
		cntr_temp = 0;
		temp = (unsigned int)(getTemperature() * 10);
	}	
	
	/* 采集距离数据 */
	if(cntr_soc == 100){
		cntr_soc = 0;
		tmp = CalDistance_t();
		if(tmp != 65535){
			temp_s = tmp*0.5*0.0001;
			distance = temp_s*tran_speed+cali_value;
		}else{
			distance = 255;
		}
	}	
	
    if(cntr_key == 10){
		cntr_key = 0;
        GetKeyStruct()->pFun();
    }
}
//
void record_Proc(void)
{
	static unsigned char record_index = 0;
	static unsigned char output_index = 0;
	/* 记录 */
	if(cntr_6s > 0){
		if(cntr_record == 200){
			cntr_record = 0;
			record_distance[record_index] = distance;
			if(++record_index == 30){
				record_index = 0;
			}
		}
	}
	/* 输出 */
	if(f_record == 1){
		if(cntr_output == 200){
			cntr_output = 0;
			if(record_distance[output_index] <= 10){
				SetDAC((DAC_limit/50.0)*255);
			}else if(record_distance[output_index] > 90){
				SetDAC(255);
			}else{
				SetDAC(((record_distance[output_index]*((50.0-DAC_limit)/800.0))+((90.0*DAC_limit-500.0)/800.0))*51);
			}
			
			if(++output_index == 30){
				output_index = 0;
				f_record = 0;
			}
		}
	}
}

//
void taskKey(void)
{
	/* 按键部分处理 */
	switch(GetKeyStruct()->value){
		/* S4 */
		case 1:
			if(cntr_6s == 0){
				if(++ui == 3){
					ui = 0;
				}
				/* 每次进入测距界面,默认距离单位为cm */
				if(ui == 0){
					dis_unit = 0;
				/* 每次进入参数设置界面,默认选择距离参数界面 */
				}else if(ui == 1){
					ui_para = 0;
				/* 每次进入工厂模式,默认选择校准值界面 */
				}else if(ui == 2){
					ui_factory = 0;
				}
			}
			GetKeyStruct()->value = 255;
			break;
		/* S5 */
		case 3:
			/* 距离界面下，调整单位切换
			   参数界面下，调整参数切换
			   工厂模式下，跳转界面切换 */
			if(cntr_6s == 0){
				if(ui == 0){
					dis_unit = ~dis_unit;
				}else if(ui == 1){
					ui_para = ~ui_para;
				}else if(ui == 2){
					if(++ui_factory == 3){
						ui_factory = 0;
					}
				}
			}
			GetKeyStruct()->value = 255;
			break;
		/* S8 */
		case 2:
			/* 加 */
			if(cntr_6s == 0){
				if(ui == 1){
					if(ui_para == 0){
						if(para_dis >= 90){
							para_dis = 10;
						}else{
							para_dis += 10;
						}
					}else{
						if(para_temp >= 80){
							para_temp = 0;
						}else{
							para_temp++;
						}
					}
				}else if(ui == 2){
					if(ui_factory == 0){
						if(cali_value >= 90){
							cali_value = -90;
						}else{
							cali_value += 5;
						}
					}else if(ui_factory == 1){
						if(tran_speed >= 9990){
							tran_speed = 10;
						}else{
							tran_speed += 10;
						}
					}else{
						if(DAC_limit >= 20){
							DAC_limit = 1;
						}else{
							DAC_limit += 1;
						}
					}
				}
				/* 记录 */
				if(ui == 0){
					cntr_6s = 6000;
				}
			}
			GetKeyStruct()->value = 255;
			break;
		/* S9 */
		case 4:
			/* 减 */
			if(cntr_6s == 0){
				if(ui == 1){
					if(ui_para == 0){
						if(para_dis <= 10){
							para_dis = 90;
						}else{
							para_dis -= 10;
						}
					}else{
						if(para_temp <= 0){
							para_temp = 80;
						}else{
							para_temp--;
						}
					}
				}else if(ui == 2){
					if(ui_factory == 0){
						if(cali_value <= -90){
							cali_value = 90;
						}else{
							cali_value -= 5;
						}
					}else if(ui_factory == 1){
						if(tran_speed <= 10){
							tran_speed = 9990;
						}else{
							tran_speed -= 10;
						}
					}else{
						if(DAC_limit <= 1){
							DAC_limit = 20;
						}else{
							DAC_limit -= 1;
						}
					}
				}
				/* 回放 */
				if(ui == 0){
					if(cntr_6s == 0){
						f_record = 1;
					}
				}
			}
			GetKeyStruct()->value = 255;		
			break;
		default:
			GetKeyStruct()->value = 255;			
			break;
	}
}


//显示处理
void taskDisplay(void)
{
	DisplayStruct* seg = GetDisplayStruct();
	static char cali_Fvalue = 0;
	
	//显示部分处理
	if(cntr_dsp < 50){
		return;
	}	
	cntr_dsp = 0;
	
	switch(ui){
		/* 测距显示界面 */
		case 0:
			if(dis_unit == 0){
				seg->dot = 1;
				seg->buf[5] = (distance >= 100)?(distance % 1000/ 100):(10);
				seg->buf[6] = (distance >= 10)?(distance % 100 / 10):(10);
			}else{
				/* 双小数点显示 */
				seg->dot = 8;
				seg->buf[5] = distance % 1000/ 100;
				seg->buf[6] = distance % 100 / 10;
			}
			
			seg->buf[0] = temp/100;
			seg->buf[1] = temp%100/10;
			seg->buf[2] = temp%10;
			seg->buf[3] = 11;
			seg->buf[4] = (distance >= 1000)?(distance / 1000):(10);
			seg->buf[7] = distance % 10;
			break;
		/* 参数显示界面 */
		case 1:
			if(ui_para == 0){
				seg->dot = 255;	
				seg->buf[0] = 12;
				seg->buf[1] = 1;
				seg->buf[2] = 10;
				seg->buf[3] = 10;
				seg->buf[4] = 10;
				seg->buf[5] = 10;
				seg->buf[6] = para_dis / 10;
				seg->buf[7] = para_dis % 10;
			}else{
				seg->dot = 255;	
				seg->buf[0] = 12;
				seg->buf[1] = 2;
				seg->buf[2] = 10;
				seg->buf[3] = 10;
				seg->buf[4] = 10;
				seg->buf[5] = 10;
				seg->buf[6] = (para_temp >= 10)?(para_temp / 10):(10);
				seg->buf[7] = para_temp % 10;
			}
			break;
		/* 工厂模式数据处理 */
		case 2:
			if(ui_factory == 0){
				seg->dot = 255;	
				seg->buf[0] = 13;
				seg->buf[1] = 1;
				seg->buf[2] = 10;
				seg->buf[3] = 10;
				seg->buf[4] = 10;
				if(cali_value >= 0){
					seg->buf[5] = 10;
					seg->buf[6] = (cali_value >= 10)?(cali_value / 10):(10);
					seg->buf[7] = cali_value % 10;
				}else{
					cali_Fvalue = -cali_value;
					if(cali_Fvalue >= 10){
						seg->buf[5] = 11;
						seg->buf[6] = (cali_Fvalue >= 10)?(cali_Fvalue / 10):(10);
						seg->buf[7] = cali_Fvalue % 10;
					}else{
						seg->buf[5] = 10;
						seg->buf[6] = 11;
						seg->buf[7] = cali_Fvalue % 10;
					}
				}
			}else if(ui_factory == 1){
				seg->dot = 255;	
				seg->buf[0] = 13;
				seg->buf[1] = 2;
				seg->buf[2] = 10;
				seg->buf[3] = 10;
				seg->buf[4] = (tran_speed >= 1000)?(tran_speed / 1000):(10);
				seg->buf[5] = (tran_speed >= 100)?(tran_speed % 1000/100):(10);
				seg->buf[6] = (tran_speed >= 10)?(tran_speed % 100 / 10):(10);
				seg->buf[7] = tran_speed % 10;
			}else{
				seg->dot = 6;	
				seg->buf[0] = 13;
				seg->buf[1] = 3;
				seg->buf[2] = 10;
				seg->buf[3] = 10;
				seg->buf[4] = 10;
				seg->buf[5] = 10;
				seg->buf[6] = DAC_limit / 10;
				seg->buf[7] = DAC_limit % 10;
			}
			break;
	}	
}